import request from '@/utils/request';
import type { Booking, BookingListData, BookingQuery, BookingStats } from '@/types/booking';

/**
 * 获取预订列表
 * @param params 
 * @returns 
 */
export const fetchBookings = (params: BookingQuery) => {
  return request<BookingListData>({
    url: '/v1/booking',
    method: 'GET',
    data: params,
  });
};

/**
 * 获取预订详情
 * @param id 
 * @returns 
 */
export const fetchBookingDetail = (id: number) => {
  return request<Booking>({
    url: `/v1/booking/${id}`,
    method: 'GET',
  });
};

/**
 * 创建预订
 * @param data 
 * @returns 
 */
export const createBooking = (data: Partial<Booking>) => {
  return request<Booking>({
    url: '/v1/booking',
    method: 'POST',
    data,
  });
};

/**
 * 更新预订
 * @param id 
 * @param data 
 * @returns 
 */
export const updateBooking = (id: number, data: Partial<Booking>) => {
  return request<Booking>({
    url: `/v1/booking/${id}`,
    method: 'PUT',
    data,
  });
};

/**
 * 更新预订状态
 * @param id 
 * @param status 
 * @returns 
 */
export const updateBookingStatus = (id: number, status: number) => {
  return request<any>({
    url: `/v1/booking/${id}/status`,
    method: 'PUT',
    data: { status },
  });
};

/**
 * 删除预订
 * @param id 
 * @returns 
 */
export const deleteBooking = (id: number) => {
  return request<any>({
    url: `/v1/booking/${id}`,
    method: 'DELETE',
  });
};

/**
 * 根据门店ID获取场地列表
 * @param storeId
 * @returns
 */
export const fetchVenuesByStore = (storeId: number) => {
  return request<{ list: any[] }>({
    url: '/v1/venue',
    method: 'GET',
    data: { store_id: storeId, pageSize: 500 },
  });
};

/**
 * 获取预订统计数据
 * @param params 
 * @returns 
 */
export const fetchBookingStats = (params: { date?: string; store_id?: number; venue_id?: number }) => {
  return request<BookingStats>({
    url: '/v1/booking/stats',
    method: 'GET',
    data: params,
  });
};

/**
 * 批量更新预订状态
 * @param ids 
 * @param status 
 * @returns 
 */
export const batchUpdateBookingStatus = (ids: number[], status: number) => {
  return request<any>({
    url: '/v1/booking/batch-status',
    method: 'PUT',
    data: { ids, status },
  });
};

/**
 * 检查时间段冲突
 * @param params 
 * @returns 
 */
export const checkTimeConflict = (params: {
  venue_id: number;
  date: string;
  start_time: string;
  end_time: string;
  exclude_booking_id?: number;
}) => {
  return request<{ hasConflict: boolean; conflictBookings: Booking[] }>({
    url: '/v1/booking/check-conflict',
    method: 'POST',
    data: params,
  });
};

/**
 * 获取可用时间段
 * @param params 
 * @returns 
 */
export const getAvailableTimeSlots = (params: {
  venue_id: number;
  date: string;
}) => {
  return request<{ timeSlots: { start_time: string; end_time: string; available: boolean }[] }>({
    url: '/v1/booking/available-slots',
    method: 'GET',
    data: params,
  });
};